package com.xiaofan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-28 23:17
 **/
@Configuration//配置类
@ComponentScan(basePackages = "com.xiaofan")//组件扫描
//@EnableAspectJAutoProxy//开启aspect代理增强的使用
@EnableTransactionManagement//开启注解开关
@PropertySource("file.properties")
public class CustomConfig {
    @Bean(value = "dataSource")
    public DruidDataSource getDataSource(){
        DruidDataSource dataSource =new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("zhangfan");
        return dataSource;
    }

    @Bean(value ="dataSourceTransactionManager" )
    public DataSourceTransactionManager get(DruidDataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
            return dataSourceTransactionManager;
    }
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DruidDataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(SqlSessionFactoryBean sqlSessionFactoryBean){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        mapperScannerConfigurer.setBasePackage("com.xiaofan.mapper");
        return mapperScannerConfigurer;

    }

}
